#include <OSAL/IO.h>

#ifdef WIN32
	#include <io.h>
	#include <stdio.h>
	#include <time.h>
	#include <string.h>
	
int osal_Unlink(const char *pathname) {
	return _unlink(pathname);
}

int osal_ExclusiveCreateFile(const char *pathname) {
	// don't know how to create a file exclusively on WIN32 platform yet 
	return 0;
}

/* wrapping unix-styled system call into win32 implementation */
struct tm *localtime_r(const time_t *timep, struct tm *result) {
	if(result == NULL)
		return localtime(timep);
	else {
		struct tm *tmpres = localtime(timep);
		memcpy(result, tmpres, sizeof(struct tm));

		return tmpres;
	}
}

int stat(const char *path, _STAT_ *buf) {
	return _stat(path, buf);
}

#else
	#include <unistd.h>
	#include <sys/types.h>
	#include <sys/stat.h>
	#include <fcntl.h>

int osal_Unlink(const char *pathname) {
	return unlink(pathname);
}

int osal_ExclusiveCreateFile(const char *pathname) {
	int fd;
	if((fd = open(pathname, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
		return -1;
	else {
		close(fd);
		return 0;
	}
}

#endif
